@echo off
chcp 65001 >nul
setlocal enabledelayedexpansion

echo ========================================
echo HiAPP-lite 绿色包打包脚本
echo ========================================
echo.

REM 设置默认构建类型
set BUILD_TYPE=Release
if "%1"=="Debug" set BUILD_TYPE=Debug
if "%1"=="debug" set BUILD_TYPE=Debug

echo 构建类型: %BUILD_TYPE%
echo.

REM 设置路径变量
set PROJECT_ROOT=%~dp0
set BUILD_DIR=%PROJECT_ROOT%build\Desktop_Qt_6_7_3_MSVC2019_64bit-%BUILD_TYPE%
set PACKAGE_DIR=%PROJECT_ROOT%\release\HiAPP-lite
set EXE_NAME=HiAPP-lite.exe

REM 检查可执行文件是否存在
if not exist "%BUILD_DIR%\%EXE_NAME%" (
    echo [错误] 找不到可执行文件: %BUILD_DIR%\%EXE_NAME%
    echo 请先运行 build.bat 构建项目
    pause
    exit /b 1
)

echo [1/6] 清理旧的打包目录...
if exist "%PACKAGE_DIR%" (
    rmdir /s /q "%PACKAGE_DIR%"
)
mkdir "%PACKAGE_DIR%"
echo 打包目录已创建: %PACKAGE_DIR%
echo.

echo [2/6] 复制可执行文件...
copy "%BUILD_DIR%\%EXE_NAME%" "%PACKAGE_DIR%\" >nul
if %ERRORLEVEL% NEQ 0 (
    echo [错误] 复制可执行文件失败
    pause
    exit /b 1
)
echo 已复制: %EXE_NAME%
echo.

echo [3/6] 使用 windeployqt 复制 Qt DLL 和依赖...
REM 尝试查找 windeployqt.exe
set WINDEPLOYQT=
if defined Qt6_DIR (
    set WINDEPLOYQT=%Qt6_DIR%\msvc2019_64\bin\windeployqt.exe
)
if not exist "%WINDEPLOYQT%" (
    REM 尝试从环境变量查找
    if defined QTDIR (
        set WINDEPLOYQT=%QTDIR%\bin\windeployqt.exe
    )
)
if not exist "%WINDEPLOYQT%" (
    REM 尝试从 PATH 中查找
    where windeployqt.exe >nul 2>&1
    if %ERRORLEVEL% EQU 0 (
        set WINDEPLOYQT=windeployqt.exe
    )
)

if not exist "%WINDEPLOYQT%" (
    echo [警告] 找不到 windeployqt.exe，将跳过 Qt DLL 自动复制
    echo 请手动复制 Qt DLL 文件，或设置 Qt6_DIR 环境变量
) else (
    echo 找到 windeployqt: %WINDEPLOYQT%
    echo 正在复制 Qt DLL 和依赖...
    "%WINDEPLOYQT%" --compiler-runtime --qmldir "%BUILD_DIR%\desktop" "%PACKAGE_DIR%\%EXE_NAME%"
    if %ERRORLEVEL% NEQ 0 (
        echo [警告] windeployqt 执行失败，可能需要手动复制 Qt DLL
    ) else (
        echo Qt DLL 和依赖已复制完成
    )
)
echo.

echo [4/6] 复制 libuv DLL...
if "%BUILD_TYPE%"=="Debug" (
    set LIBUV_DLL=%PROJECT_ROOT%third_party\libuv\x64-windows\debug\bin\uv.dll
) else (
    set LIBUV_DLL=%PROJECT_ROOT%third_party\libuv\x64-windows\bin\uv.dll
)

if exist "%LIBUV_DLL%" (
    copy "%LIBUV_DLL%" "%PACKAGE_DIR%\" >nul
    if %ERRORLEVEL% EQU 0 (
        echo 已复制: uv.dll
    ) else (
        echo [警告] 复制 libuv DLL 失败
    )
) else (
    echo [警告] 找不到 libuv DLL: %LIBUV_DLL%
)
echo.

echo [5/6] 复制 QML 文件...
if exist "%BUILD_DIR%\desktop" (
    xcopy /E /I /Y "%BUILD_DIR%\desktop" "%PACKAGE_DIR%\desktop" >nul
    if %ERRORLEVEL% EQU 0 (
        echo 已复制 QML 文件到 desktop 目录
    ) else (
        echo [警告] 复制 QML 文件失败
    )
) else (
    echo [警告] 找不到 QML 文件目录: %BUILD_DIR%\desktop
)
echo.

echo [6/6] 复制 Visual C++ 运行时 DLL (如果需要)...
REM 检查是否需要复制 MSVC 运行时 DLL
REM 通常 windeployqt --compiler-runtime 已经处理了，但为了确保，我们检查一下
set MSVC_RUNTIME_FOUND=0
if exist "%PACKAGE_DIR%\msvcp*.dll" set MSVC_RUNTIME_FOUND=1
if exist "%PACKAGE_DIR%\vcruntime*.dll" set MSVC_RUNTIME_FOUND=1

if %MSVC_RUNTIME_FOUND% EQU 0 (
    echo [信息] MSVC 运行时 DLL 可能已通过 windeployqt 复制，或需要用户安装 Visual C++ Redistributable
    echo 如果程序无法运行，请安装 Microsoft Visual C++ Redistributable
) else (
    echo MSVC 运行时 DLL 已包含在包中
)
echo.

echo ========================================
echo 打包完成！
echo ========================================
echo.
echo 打包目录: %PACKAGE_DIR%
echo.
echo 打包内容:
dir /B "%PACKAGE_DIR%"
echo.
echo 提示:
echo - 如果程序无法运行，请检查是否缺少 DLL
echo - 可以使用 Dependency Walker 或 Dependencies 工具检查依赖
echo - 确保目标系统已安装 Visual C++ Redistributable（如果未包含运行时 DLL）
echo.
pause

